﻿/*******************************************************************************
 * Copyright © Chris Watson 2013                                               *
 *                                                                             *
 * This document is a part of the source code and related artefacts for Chris  *
 * Watson's Desktop Wallpaper Guard, a software application which will detect  *
 * when another process removes the wallpaper from a Windows desktop and       *
 * restore an image of the user's choice.                                      *
 *                                                                             *
 * Author:                                                                     *
 *   Chris Watson       [mailto:chris@watson.me.uk]                            *
 * Date:                                                                       *
 *   17 August 2013                                                            *
 ******************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Watson.BleaknessEliminator.Util;
using System.Globalization;
using Watson.BleaknessEliminator.Properties;
using System.Diagnostics;

namespace Watson.BleaknessEliminator
{
    /// <summary>
    /// The About Box form for the Bleakness Eliminator application.
    /// </summary>
    internal partial class AboutForm : Form
    {
        #region Private Properties

        /// <summary>
        /// Gets the assembly description.
        /// </summary>
        /// <value>
        /// The assembly description.
        /// </value>
        private string AssemblyDescription
        {
            get
            {
                object[] attributes = 
                    Assembly.GetExecutingAssembly()
                            .GetCustomAttributes(
                                typeof(AssemblyDescriptionAttribute), false);
                
                if (attributes.Length == 0)
                {
                    return "";
                }
                
                return 
                    ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Initialises the displayed form items.
        /// </summary>
        private void InitialiseDisplay()
        {
            // Set window title.
            //
            this.Text = String.Format(CultureInfo.CurrentCulture,
                "About {0}", AssemblyInfo.Default.DisplayName);

            // Set picture.
            //
            Icon icon48 = new Icon(Resources.Icon, new Size(48, 48));

            this.logoPictureBox.Image = icon48.ToBitmap();

            // Set product name label.
            //
            this.lblProductName.Text = AssemblyInfo.Default.DisplayName;

            // Set product version label.
            //
            this.lblVersion.Text = String.Format(CultureInfo.CurrentCulture,
                "Version {0}", AssemblyInfo.Default.Version);

            // Set copyright label.
            //
            this.lblCopyright.Text = AssemblyInfo.Default.Copyright;

            // Set website uri link label.
            //
            this.lnklblWebsiteUri.Text =
                AssemblyInfo.Default.WebsiteUri.ToString();

            // Set description label.
            //
            this.textBoxDescription.Text = AssemblyDescription;
        }


        /// <summary>
        /// Handles the LinkClicked event of the linkLabel1 control. Navigates
        /// to the website for the application's project.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="LinkLabelLinkClickedEventArgs"/> instance containing
        /// the event data.
        /// </param>
        private void lnklblWebsiteUri_LinkClicked(object sender,
            LinkLabelLinkClickedEventArgs e)
        {
            // Obtain URI.
            //
            string uri = AssemblyInfo.Default.WebsiteUri.ToString();

            // Show busy.
            //
            Cursor = Cursors.AppStarting;
            
            try
            {
                // Navigate to URI.
                //
                Process.Start(uri);
            }
            catch (Exception ex)
            {
                // URI navigation failed - show message to user with error.
                //
                string messageBoxText = 
                    String.Format(CultureInfo.CurrentCulture,
                        "Failed to navigate to URI: '{0}'.\r\n\r\n{1}: {2}",
                        uri,
                        ex.GetType().FullName,
                        ex.Message);
                
                MessageBox.Show(
                    text:    messageBoxText,
                    caption: "URI Navigation Failed",
                    buttons: MessageBoxButtons.OK,
                    icon:    MessageBoxIcon.Warning);
            }
            finally
            {
                // Show not busy.
                //
                Cursor = Cursors.Default;
            }
        }


        /// <summary>
        /// Handles the Click event of the okButton control. Closes the About
        /// Box form.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The <see cref="EventArgs"/> instance containing the event data.
        /// </param>
        private void okButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;

            Close();
        }

        #endregion

        #region Internal Constructors

        /// <summary>
        /// Initialises a new instance of the <see cref="AboutForm"/> class.
        /// </summary>
        internal AboutForm()
        {
            // Set Form Designer properties.
            //
            InitializeComponent();

            // Initialise form items.
            //
            InitialiseDisplay();
        }
        
        #endregion
    }
}
